<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('BookStore2[\'TemplateName\']')">
        <el-input v-model="listContext.name" @keyup.enter.native="fetchData" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="fetchData">
          <i class="el-icon-search" />
        </el-button>
      </el-form-item>
      <el-button
        v-if="checkPermission('BookStore2MessageTemplate.Create')"
        type="primary"
        style="float: right"
        @click="openCreateDialog"
      >
        <i class="el-icon-plus" />{{ $t('BookStore2[\'NewMessageTemplate\']') }}
      </el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('BookStore2[\'LoadingWithThreeDot\']')"
      :empty-text="$t('BookStore2[\'NoDataAvailableInDatatable\']')"
      border
      fit
      highlight-current-row
      height="calc(100vh - 225px)"
      @sort-change="sortChange"
    >
      <el-table-column :label="$t('BookStore2[\'TemplateName\']')" prop="name" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Display:Remark\']')" prop="remark">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'TemplateBlobName\']')" prop="template.blobName">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-download" type="primary" circle @click="downloadFile(scope.row)" />
          {{ scope.row.template.fileName }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Display:CreationTime\']')" prop="creationTime" sortable="custom">
        <template slot-scope="scope">
          {{ scope.row.creationTime | dateYMDHMSFormat }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('BookStore2[\'Actions\']')">
        <template slot-scope="scope">
          <el-dropdown trigger="click" @command="clickActionDropDown">
            <el-button type="primary">
              <i class="el-icon-setting" />
              {{ $t('BookStore2[\'Actions\']') }}
              <i class="el-icon-arrow-down" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="checkPermission('BookStore2MessageTemplate.Update')" :command="{ action: 'edit', row: scope.row }">{{ $t('BookStore2[\'Edit\']') }}</el-dropdown-item>
              <el-dropdown-item v-if="checkPermission('BookStore2MessageTemplate.Delete')" :command="{ action: 'delete', row: scope.row }">{{ $t('BookStore2[\'Delete\']') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top: 10px">
      <el-pagination
        :page-sizes="pageContext.pageSizes"
        :page-size.sync="listContext.maxResultCount"
        :current-page.sync="pageContext.currentPage"
        :total="pageContext.totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="handleCurrentChange"
      />
    </div>
    <create-or-edit-message-template :visible.sync="createOrEditDialogVisible" :context="createOrEditDialogCtx" @saveSucceed="fetchData" />
  </div>
</template>

<script>
import { getList, deleteById } from '@/api/messageTemplate/messageTemplate'
import { getDownloadHref } from '@/api/blob/blob'
import CreateOrEditMessageTemplate from '@/views/messageTemplate/components/createOrEditMessageTemplate'
import { checkPermission } from '@/utils/abp'
export default {
  name: 'MessageTemplate',
  components: { CreateOrEditMessageTemplate },
  data() {
    return {
      list: null,
      listLoading: false,
      pageContext: {
        pageSizes: [10, 25, 50, 100],
        totalCount: 0,
        currentPage: 1
      },
      listContext: {
        title: '',
        sorting: '',
        skipCount: 0,
        maxResultCount: 10
      },
      createOrEditDialogCtx: {
        id: '',
        title: '',
        actionMode: ''
      },
      createOrEditDialogVisible: false,
      dataDict: {}
    }
  },
  async created() {
    this.fetchData()
  },
  methods: {
    checkPermission,
    fetchData() {
      this.listLoading = true
      getList(this.listContext).then((response) => {
        this.list = response.items
        this.pageContext.totalCount = response.totalCount
        this.listLoading = false
      })
    },
    handleCurrentChange(currentPage) {
      this.listContext.skipCount = (currentPage - 1) * this.listContext.maxResultCount
      this.fetchData()
    },
    sortChange({ prop, order }) {
      this.listContext.sorting = ''
      if (order) {
        this.listContext.sorting = prop + ' ' + order
      }
      this.fetchData()
    },
    clickActionDropDown(ctx) {
      switch (ctx.action) {
        case 'edit':
          this.openEditDialog(ctx.row.id)
          break
        case 'delete':
          this.$confirm(this.$t('BookStore2[\'AreYouSure\']')).then(async _ => {
            await deleteById(ctx.row.id)
            this.$message({
              message: this.$t('BookStore2[\'Ok\']'),
              type: 'success'
            })
            this.fetchData()
          }).catch(_ => {})
          break
      }
    },
    openCreateDialog() {
      this.createOrEditDialogVisible = true
      this.createOrEditDialogCtx = {
        title: this.$t('BookStore2[\'NewMessageTemplate\']'),
        actionMode: 'create'
      }
    },
    openEditDialog(id) {
      this.createOrEditDialogVisible = true
      this.createOrEditDialogCtx = {
        title: this.$t('BookStore2[\'Edit\']'),
        actionMode: 'edit',
        id: id
      }
    },
    downloadFile(row) {
      window.location.href = getDownloadHref(row.template.blobContainerName, row.template.blobName, row.template.fileName)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

